package com.rzico.quartz.job;

import com.rzico.basics.entity.Supplier;
import com.rzico.basics.service.SupplierService;
import com.rzico.core.entity.SysJob;
import com.rzico.core.service.SysJobService;
import com.rzico.order.entity.Shipping;
import com.rzico.order.entity.ShippingItem;
import com.rzico.order.plugin.JDPluginService;
import com.rzico.order.service.ShippingItemService;
import com.rzico.order.service.ShippingService;
import com.rzico.util.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 京东订单物流信息更新
 */
public class JDTrackNoTask implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private ShippingService shippingService;

    @Autowired
    protected ShippingItemService shippingItemService;

    @Autowired
    protected JDPluginService jDPluginService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {
            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }
            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里开始计算访问统计
            /**
             * 第一步，查询所有京东商品且无运单号的送货单项记录，若无记录，则跳过
             */
            //获取京东供应商id
            Long supplierId = 0L;
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("name","京东");
            List<Supplier> supplierList = supplierService.selectList(params);
            if (0 < supplierList.size()) {
                supplierId = supplierList.get(0).getId();
            }
            List<ShippingItem> shippingItemList = new ArrayList<ShippingItem>();
            /**
             * 第二步，查询京东供应商且已下单的发货单，再查询出发货单项，进行物流判断
             */
            Map<String,Object> shipParams = new HashMap<>();
            shipParams.put("supplierId", supplierId);
            shipParams.put("shippingStatus", 1);
            List<Shipping> shipList = shippingService.selectList(shipParams);
            for (Shipping shipping : shipList) {
                if (StringUtils.isEmpty(shipping.getTranSn())) {
                    continue;
                }
                //根据发货单查询该单下的发货单项
                List<ShippingItem> list = shippingItemService.getItemList(shipping.getId());
                for (ShippingItem si : list) {
                    if (StringUtils.isEmpty(si.getTrackingNo()) || StringUtils.isEmpty(si.getSubTranSn())) {
                        shippingItemList.add(si);
                    }
                }
            }
            if (0 < shippingItemList.size()) {
                jDPluginService.updateShippingItemTrackno(shippingItemList);
                /**
                 * 判断所有有京东订单号的送货单，判断该送货单是否可更新为已发货状态
                 */
                jDPluginService.updateShippingStatus(supplierId);
            }
            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }
        catch (Exception e) {
            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }

    }
}
